################################################################################
#
# pfeng
#
################################################################################

S32_PFE_VERSION = 1.0
S32_PFE_SITE = $(TOPDIR)/package/nxp/s32/s32-pfe/pfeng
S32_PFE_SITE_METHOD := local
S32_PFE_INSTALL_TARGET := YES

# S32_LIBFCI_DIRS = $(@D)/sw/xcfi/libfci
# S32_LIBFCI_CLI_DIRS = $(@D)/sw/libfci_cli

MODULE_DIRS = $(TARGET_DIR)/lib/modules/$(LINUX_VERSION_PROBED)
S32_PFE_INSTALL_TARGET_DIR = $(MODULE_DIRS)/kernel/drivers/net/ethernet/nxp/pfe

ifeq ($(BR2_TOOLCHAIN_BUILDROOT),y)
S32_PFE_PLATFORM = $(GNU_TARGET_NAME)
else
S32_PFE_PLATFORM = $(TOOLCHAIN_EXTERNAL_PREFIX)
endif

S32_PFE_MAKE_FLAGS = \
	KERNELDIR=$(LINUX_DIR) \
	PLATFORM=$(S32_PFE_PLATFORM)

S32_PFE_MASTER_MAKE_FLAGS = \
	$(S32_PFE_MAKE_FLAGS) \
	PFE_CFG_MULTI_INSTANCE_SUPPORT=1 \
	PFE_CFG_PFE_MASTER=1

S32_PFE_SLAVE_MAKE_FLAGS = \
	$(S32_PFE_MAKE_FLAGS) \
	PFE_CFG_MULTI_INSTANCE_SUPPORT=1 \
	PFE_CFG_PFE_MASTER=0

S32_LIBFCI_MAKE_FLAGS = \
	TARGET_OS=LINUX \
	PLATFORM=$(S32_PFE_PLATFORM) \
	ARCH=$(ARCH)

S32_LIBFCI_CLI_MAKE_FLAGS = \
	TARGET_OS=LINUX \
	PLATFORM=$(S32_PFE_PLATFORM)

define S32_PFE_BUILD_CMDS
	$(MAKE1) $(S32_PFE_MAKE_FLAGS) -C $(@D)/sw/linux-pfeng drv-clean
	$(MAKE1) $(S32_PFE_MAKE_FLAGS) -C $(@D)/sw/linux-pfeng all;
	if [ "$(BR2_PACKAGE_S32G_PFENG_MASTER_SLAVE)" = "y" ]; then \
		$(MAKE1) $(S32_PFE_MASTER_MAKE_FLAGS) -C $(@D)/sw/linux-pfeng all; \
		$(MAKE1) $(S32_PFE_SLAVE_MAKE_FLAGS) -C $(@D)/sw/linux-pfeng all; \
	fi
	if [ "$(BR2_PACKAGE_S32G_PFENG_USE_LIBFCI)" = "y" ]; then \
		$(TARGET_MAKE_ENV) $(MAKE) ${S32_LIBFCI_MAKE_FLAGS} -C $(@D)/sw/xfci/libfci linux; \
	fi
	if [ "$(BR2_PACKAGE_S32G_PFENG_USE_LIBFCI_CLI)" = "y" ]; then \
		$(TARGET_MAKE_ENV) $(MAKE) ${S32_LIBFCI_CLI_MAKE_FLAGS} -C $(@D)/sw/libfci_cli all; \
	fi
endef

ifneq ($(BR2_PACKAGE_S32G_PFENG_DEBUG),y)

define S32_PFE_POST_STRIP_CMDS
	$(TARGET_STRIP) --strip-debug $(@D)/sw/linux-pfeng/pfeng.ko
	if [ "$(BR2_PACKAGE_S32G_PFENG_MASTER_SLAVE)" = "y" ]; then \
		$(TARGET_STRIP) --strip-debug $(@D)/sw/linux-pfeng/pfeng-slave.ko; \
	fi
endef
S32G_PFE_POST_BUILD_HOOKS += S32G_PFE_POST_STRIP_CMDS

endif #end BR2_PACKAGE_S32G_PFENG_DEBUG=y

# $(HOST_DIR)/sbin/depmod -a -b $(TARGET_DIR) $(TARGET_DIR)/lib/modules/$(LINUX_VERSION_PROBED)
define S32_PFE_INSTALL_TARGET_CMDS
	$(INSTALL) -D -m 0755 $(@D)/sw/linux-pfeng/pfeng.ko $(S32_PFE_INSTALL_TARGET_DIR)/pfeng.ko
	if [ "$(BR2_PACKAGE_S32G_PFENG_MASTER_SLAVE)" = "y" ]; then \
		$(INSTALL) -D -m 0755 $(@D)/sw/linux-pfeng/pfeng-slave.ko $(S32_PFE_INSTALL_TARGET_DIR)/pfeng-slave.ko; \
	fi

	$(INSTALL) -D -m 0755 $(@D)/sw/xfci/libfci/build/$(S32_PFE_PLATFORM)-release/libfci.a $(TARGET_DIR)/usr/lib/libfci.a
	$(INSTALL) -D -m 0755 $(@D)/sw/libfci_cli/libfci_cli $(TARGET_DIR)/usr/sbin/libfci_cli
endef


###############################################################################################################
# pfe firmware
###############################################################################################################
ifeq ($(BR2_PACKAGE_S32G_PFENG_FIRMWARE),y)

S32_PFE_FIRMWARE_VERSION = $(call qstrip,$(BR2_PACKAGE_S32G_PFENG_FIRMWARE_VERSION))
S32_PFE_FIRMWARE_DIR = $(TOPDIR)/package/nxp/s32/s32-pfe/firmware/PFE-FW_S32G_$(S32_PFE_FIRMWARE_VERSION)
S32_PFE_FIRMWARE_CLASS = $(S32_PFE_FIRMWARE_DIR)/s32g_pfe_class.fw
S32_PFE_FIRMWARE_UTIL = $(S32_PFE_FIRMWARE_DIR)/s32g_pfe_util.fw

define PFE_FIRMWARE_INSTALL_TO_TARGET
	@$(call MESSAGE,"Install s32g pfe firmware")
	$(INSTALL) -D -m 0644 $(S32_PFE_FIRMWARE_CLASS) $(TARGET_DIR)/lib/firmware/s32g_pfe_class.fw
	$(INSTALL) -D -m 0644 $(S32_PFE_FIRMWARE_UTIL) $(TARGET_DIR)/lib/firmware/s32g_pfe_util.fw
	$(INSTALL) -D -m 0644 $(S32_PFE_FIRMWARE_CLASS) $(BINARIES_DIR)/s32g_pfe_class.fw
	$(INSTALL) -D -m 0644 $(S32_PFE_FIRMWARE_UTIL) $(BINARIES_DIR)/s32g_pfe_util.fw
endef
S32_PFE_POST_INSTALL_TARGET_HOOKS += PFE_FIRMWARE_INSTALL_TO_TARGET

endif # end BR2_PACKAGE_S32G_PFENG_FIRMWARE


$(eval $(generic-package))
